package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.base.fireEngine.dto.FireEngineDto;
import com.dykj.fireFighting.api.base.fireEngine.entity.FireEngine;
import com.dykj.fireFighting.api.base.fireEngine.service.IFireEngineService;
import com.dykj.fireFighting.api.base.fireEngine.vo.EXLIVEFireEngineLocsHistoryVo;
import com.dykj.fireFighting.api.base.fireEngine.vo.EXLIVEFireEngineLocsVo;
import com.dykj.fireFighting.api.base.fireEngine.vo.FireEngineVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 消防车接口
 *
 * @Author JacksonTu
 * @Date 2021-11-12 13:47:40
 */
@Slf4j
@Api(value = "消防车接口", tags = {"消防车接口"})
@RestController
@RequestMapping("/fireEngine")
public class FireEngineController {

    @DubboReference
    private IFireEngineService fireEngineService;


    /**
     * 消防车列表
     *
     * @return
     */
    @AutoLog(value = "消防车-分页列表查询")
    @ApiOperation(value = "消防车分页列表", notes = "消防车分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<FireEngineVo>> treeGrid(FireEngineDto fireEngineDto) {
        PageVo<FireEngineVo> page = fireEngineService.queryList(fireEngineDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防车信息
     */
    @AutoLog(value = "消防车-获取消防车信息")
    @ApiOperation(value = "获取消防车信息", notes = "获取消防车信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<FireEngine> info(@PathVariable("id") String id) {
        FireEngine fireEngine = fireEngineService.getById(id);
        return CommonResult.success(fireEngine);
    }

    /**
     * 保存消防车信息
     */
    @AutoLog(value = "消防车-保存消防车信息")
    @ApiOperation(value = "保存消防车信息", notes = "保存消防车信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody FireEngine fireEngine) {
        try {
            fireEngine.setCreateTime(new Date());
            fireEngine.setUpdateTime(new Date());
            fireEngineService.save(fireEngine);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防车信息
     */
    @AutoLog(value = "消防车-修改消防车信息")
    @ApiOperation(value = "修改消防车信息", notes = "修改消防车信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody FireEngine fireEngine) {
        try {
            fireEngine.setUpdateTime(new Date());
            fireEngineService.updateById(fireEngine);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防车信息
     */
    @AutoLog(value = "消防车-删除消防车信息")
    @ApiOperation(value = "删除消防车信息", notes = "删除消防车信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            fireEngineService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 车辆最新位置
     *
     * @param carNum
     * @return
     */
    @AutoLog(value = "消防车-车辆最新位置")
    @ApiOperation(value = "车辆最新位置", notes = "车辆最新位置")
    @PostMapping("/loadLocation")
    public CommonResult loadLocation(String carNum) {
        try {
            EXLIVEFireEngineLocsVo exliveFireEngineLocsVo = fireEngineService.loadLocation(carNum);
            return CommonResult.success(exliveFireEngineLocsVo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("车辆最新位置获取失败，请联系管理员");
        }
    }

    /**
     * 车辆最新位置
     *
     * @param carNum
     * @return
     */
    @AutoLog(value = "消防车-轨迹回放")
    @ApiOperation(value = "轨迹回放", notes = "轨迹回放")
    @PostMapping("/loadHistory")
    public CommonResult loadHistory(String carNum, Long bTime, Long eTime) {
        try {
            List<EXLIVEFireEngineLocsHistoryVo> locsHistoryList = fireEngineService.loadHistory(carNum, bTime, eTime);
            return CommonResult.success(locsHistoryList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("轨迹回放获取失败，请联系管理员");
        }
    }

    /**
     * 车辆最新位置
     *
     * @param
     * @return
     */
    @AutoLog(value = "消防车-车辆最新位置")
    @ApiOperation(value = "车辆最新位置", notes = "车辆最新位置")
    @PostMapping("/batchLoadLocation")
    public CommonResult batchLoadLocation() {
        try {
            fireEngineService.batchLoadLocation();
            return CommonResult.success("车辆最新位置获取成功,webSocket推送");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("车辆最新位置获取失败，请联系管理员");
        }
    }
}
